package com.sita.tboard.roadtrust.gridimage;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.sita.bike.support.GlobalContext;
import com.sita.tboard.view.ImageModel;
import com.sita.tboard.view.NineGridAdapter;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by xiaodong on 16/3/22.
 */
public class RtResourceGalleryAdapter extends NineGridAdapter<ImageModel> {
    public RtResourceGalleryAdapter(Context mContext, List<ImageModel> mList) {
        super(mContext, mList);
    }

    @Override
    public int getCount() {
        return getData() != null ? getData().size() : 0;
    }

    @Override
    public String getUrl(int position) {
        ImageModel imageModel = getItem(position);
        return imageModel != null ? imageModel.getUrl() : null;
    }

    @Override
    public ImageModel getItem(int position) {
        List<ImageModel> data = getData();
        return data != null ? data.get(position) : null;
    }

    @Override
    public View getView(final int i, View view) {
        ImageView iv;
        if (view != null && view instanceof ImageView) {
            iv = (ImageView) view;
        } else {
            iv = new ImageView(getContext());
        }
        iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
        iv.setBackgroundColor(getContext().getResources().getColor((android.R.color.transparent)));
        String url = getUrl(i);
        if (getCount() == 1){
            ImageModel imageModel = getItem(i);
            Picasso.with(GlobalContext.getGlobalContext())
                    .load(getUrl(i))
                    .placeholder(new ColorDrawable(Color.parseColor("#f5f5f5")))
                    .resize(imageModel.getWidth(), imageModel.getHeight())
                    .centerCrop()
                    .into(iv);
        } else {
            Picasso.with(GlobalContext.getGlobalContext())
                    .load(getUrl(i))
                    .placeholder(new ColorDrawable(Color.parseColor("#f5f5f5")))
                    .fit().centerInside()
                    .into(iv);
        }
        if (!TextUtils.isEmpty(url)) {
            iv.setTag(url);
        }
        return iv;
    }

}
